﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using CRM.Controller;
using CRM.Entity.Extra;

namespace CRM.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.IsAuthenticated)
                {
                    Response.Redirect("/welcome.aspx");
                }
            }
        }


        protected void btn_submit_Click(object sender, EventArgs e)
        {          

            string userName = username.Value.Trim();
            string pwd = password.Value.Trim();
            string validateCode = valcode.Value.Trim();


            string validateCodeKey = "validateCodeKey";
            if (Session[validateCodeKey].ToString() != validateCode)
            {
                lit_msg.Text = "验证码输入错误";
                return;
            }

            if(string.IsNullOrWhiteSpace(userName))
            {
                lit_msg.Text = "用户名不能为空";
                return;
            }


            UserInfoController controller = new UserInfoController();

            var userInfo = controller.GetSingle(userName);
            if (userInfo == null)
            {
                lit_msg.Text = "用户名不存在";
                return;
            }

            if ( userInfo.Password == pwd)
            {
                FormsAuthentication.SetAuthCookie(userName, false);
                Response.Redirect("/welcome.aspx");
            }
            else
            {
                lit_msg.Text = "用户名或密码错误";
            }
        }
    }
}